home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9014 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: newshost.cyberramp.net!news
  2. From: sinan@cyberramp.net (John Noland)
  3. Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1
  4. Subject: Re: GOTO controversy
  5. Date: 6 Mar 1996 23:56:13 GMT
  6. Organization: Prose Software
  7. Message-ID: <4hl8mt$4po@newshost.cyberramp.net>
  8. References: <rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU>
  9. NNTP-Posting-Host: ramp3-25.cyberramp.net
  10. X-Newsreader: WinVN 0.99.5
  11.  
  12. >Robert C Shields (rcshlds@mailserv.mta.ca) wrote:
  13. > I am trying to get a feel for how people feel about the GOTO statement... 
  14. >: please post..
  15. >
  16. >Somebody at school saw a goto in my code and freaked. Then again, this 
  17. >person (who shall remain nameless) is used to machines with 100MB of 
  18. >memory and dual SPARC CPU's. I on the other hand am not.
  19.  
  20. >The code in question was a routine that allocated various resources. 
  21. >Below is an example of such a thing (this time using OS/2 & C). I use Goto's 
  22. >to clean up the mess in the event of an error. 
  23. >
  24. >Example for a good use of goto:
  25. >---                    ????
  26. >------------------------------------------------------------------------------
  27. >   HEV    hev1, hev2, hev3;     /* Event semaphores */
  28. >   HMTX   hmtx;                 /* Mutex semaphore  */ 
  29. >   void  *ptr;              
  30.  
  31. >   if (!DosCreateEventSem(0, &hev1, 0, FALSE))
  32. >       goto hev1_failed;
  33. >
  34. >   if (!DosCreateEventSem(0, &hev2, 0, FALSE))
  35. >       goto hev2_failed;
  36.  
  37. >   if (!DosCreateEventSem(0, &hev3, 0, FALSE))
  38. >       goto hev3_failed;
  39. >
  40. >   if (!DosCreateMutexSem(0, &hmtx, 0, FALSE))
  41. >       goto hmtx_failed;
  42. >
  43. >   if ((ptr = malloc(SOME_SIZE)) == NULL)
  44. >       goto malloc_failed;
  45. >
  46. >   /* Do some stuff here */
  47. >   return TRUE; /* We did okay */
  48. >
  49. >malloc_failed:
  50. >   DosCloseMutexSem(hmtx);
  51. >hmtx_failed:
  52. >   DosCloseEventSem(hev3);
  53. >hev3_failed:
  54. >   DosCloseEventSem(hev2);
  55. >hev2_failed:
  56. >   DosCloseEventSem(hev1);
  57. >hev1_failed:
  58. >   return FALSE;
  59.  
  60.  
  61. The above is NOT an example of a good use for the goto statement. A lot
  62. of programmers hate goto with fanatical fervor. I personally feel they 
  63. can make for better code in certain situations. The first situation would
  64. be to eliminate multiple return statements. A goto branch to a SINGLE label
  65. is greatly preferable to multiple return statements with several exit points.
  66. A subroutine with a single exit point is a lot easier to maintain than one 
  67. with several exit points. The second situation would be to break out of a 
  68. nested loop. The alternative to a goto in this situation is to use of flag
  69. of some kind (while(more_data)), but this tends to make the code larger and
  70. harder to read. Some more guidelines to follow for goto's are:
  71.  
  72.  DON'T use a goto unless it's the only solution to the problem.
  73.  You have many alternatives to a goto in your above code. 
  74.  
  75.  A subroutine should have at most one label.
  76.  
  77.  All goto's should be above that label in the code.
  78.  
  79.  The label should be in the same code block or at a more outer nesting level
  80.  than the goto itself.
  81.  
  82. Your code could be easily reorganized to not use a goto.
  83.  
  84. -John
  85.  
  86.